package software.hust.com.hust_last.network;

import android.text.TextUtils;

import java.util.HashMap;
import java.util.Map;

import software.hust.com.hust_last.callback.IResponseCallback;
import software.hust.com.hust_last.util.AesUtil;
import software.hust.com.hust_last.util.AppConfig;
import software.hust.com.hust_last.util.HttpUtil;

public abstract class BaseData {
    private HttpUtil mHttpUtil = HttpUtil.getInstance();

    public void request(IResponseCallback callback) {
        request(null, callback);
    }

    /**
     * 一般情况下用这个方法，以便能通过tag取消网络请求
     *
     * @param tag 用来取消网络请求
     */
    public void request(Object tag, IResponseCallback callback) {
        mHttpUtil.post(tag, AppConfig.BASE_URL + getPath(), getParams(), callback);
    }

    public void requestGet(Object tag, IResponseCallback callback) {
        mHttpUtil.get(tag, AppConfig.BASE_URL + getPath(), getParams(), callback);
    }

    private Map<String, String> getParams() {
        Map<String, String> params = new HashMap<>();
        setParams(params);
        encode(params);
        return params;
    }

    private void encode(Map<String, String> params) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            params.put(entry.getKey(), AesUtil.encrypt(entry.getValue()));
        }
    }

    protected boolean isNotEmpty(String str) {
        return !TextUtils.isEmpty(str);
    }

    protected abstract String getPath();

    protected abstract void setParams(Map<String, String> params);
}
